package com.ljy.cart.interceptor;


import com.ljy.cart.vo.UserInfoTo;
import com.ljy.common.constant.AuthServerConstant;
import com.ljy.common.constant.CartConstant;
import com.ljy.common.vo.MemberRespVo;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/*
* 在执行目标方法之前，判断目标的登陆状态，封装传递给controller目标请求
*
* */
public class CartInterceptor implements HandlerInterceptor {

    /*
    * @TODO Thread-Local 相同线程，共享数据
    * @author ljy
    * @time 2022-01-29 10:55:22
    * @method
    * @param param
    * @return
    */
    public static ThreadLocal<UserInfoTo> threadLocal = new ThreadLocal<>();


    /*
    * @TODO 目标方法执行前,判断用户是否登录，如果用户已登陆，设置user-key
    * @author ljy
    * @time 2022-01-29 10:29:05
    * @method
    * @param param
    * @return
    */

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) {
        UserInfoTo userInfoTo = new UserInfoTo();
        //此session被springSession包装过，所以从数据库中读取
        HttpSession session = request.getSession();
        MemberRespVo member = (MemberRespVo)session.getAttribute(AuthServerConstant.LOGIN_SUCCESS_USER);
        if(member != null){
            //用户登录
            userInfoTo.setUserId(member.getId());
        }
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0){
            for (Cookie cookie:cookies){
                //user-key
                String name = cookie.getName();
                if (name.equals(CartConstant.TEMP_USER_COOKIE_NAME)){
                    userInfoTo.setUserKey(cookie.getValue());
                    //说明cookie中有临时用户
                    userInfoTo.setTempUser(true);
                }
            }
        }
        //如果userInfo为空，说明没有临时用户，需要为其自定义临时用户
        if (userInfoTo.getUserKey()==null){
            String uuid = UUID.randomUUID().toString();
            userInfoTo.setUserKey(uuid);
        }
        //将信息放入ThreadLocal中
        threadLocal.set(userInfoTo);
        return true;
    }


    /*
    * @TODO 业务执行之后，让浏览器保存cookie,分配临时用户
    * @author ljy
    * @time 2022-01-29 10:05:38
    * @method
    * @param param
    * @return
    */

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        //拿到方法执行前封装的用户信息
        UserInfoTo userInfoTo = threadLocal.get();
        Cookie cookie = new Cookie(CartConstant.TEMP_USER_COOKIE_NAME, userInfoTo.getUserKey());
        //如果有临时用户信息
        if(!userInfoTo.isTempUser()){
            //设置过期时间和作用域
            cookie.setDomain("jymail.com");
            cookie.setMaxAge(CartConstant.TEMP_USER_COOKIE_TIMEOUT);
            response.addCookie(cookie);
        }
    }
}
